package com.thb.pay.mchant.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.thb.pay.api.merchant.MchInfoServiceApi;
import com.thb.pay.api.merchant.vo.req.MchInfoPageReq;
import com.thb.pay.api.merchant.vo.req.MchInfoReq;
import com.thb.pay.api.merchant.vo.resp.MchInfoVo;
import com.thb.pay.core.exception.RRException;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.entity.SuperEntity;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.mchant.entity.MchInfo;
import com.thb.pay.mchant.service.MchInfoService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 商户-信息表 前端控制器
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@Slf4j
@RestController
public class MchInfoController implements MchInfoServiceApi {

    @Autowired
    private MchInfoService mchInfoService;

    @Override
    public PageResult<MchInfoVo> findPage(@RequestBody MchInfoPageReq pageReq) {
        IPage<MchInfo> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<MchInfo> pageResult = mchInfoService.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, MchInfoVo.class);
    }

    @Override
    public List<MchInfoVo> findAllList() {
        List<MchInfoVo> resultDataList = Lists.newArrayList();
        List<MchInfo> targetDbResultList = mchInfoService.selectList(null);
        targetDbResultList.forEach(dbResult -> {
            MchInfoVo resultData = new MchInfoVo();
            BeanUtil.copyProperties(dbResult, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public List<MchInfoVo> findListByIds(@RequestBody List<Long> ids) {
        List<MchInfoVo> resultDataList = Lists.newArrayList();
        List<MchInfo> resDataList = mchInfoService.selectBatchIds(ids);
        resDataList.forEach(payProduct -> {
            MchInfoVo resultData = new MchInfoVo();
            BeanUtil.copyProperties(payProduct, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public MchInfoVo getById(@RequestParam("id") Long id) {
        MchInfoVo resultData = null;
        MchInfo dbResult = mchInfoService.selectById(id);
        if (dbResult != null) {
            resultData = new MchInfoVo();
            BeanUtil.copyProperties(dbResult, resultData);
        }
        return resultData;
    }

    @Override
    public void add(@RequestBody MchInfoReq mchInfoReq) {
        LambdaQueryWrapper<MchInfo> wrapper = new LambdaQueryWrapper();
        wrapper.eq(MchInfo::getUserName, mchInfoReq.getUserName());
        List<MchInfo> targetDbResultList = mchInfoService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            throw RRException.build("pay.scene.exist");
        }
        MchInfo targetDbData = new MchInfo();
        BeanUtil.copyProperties(mchInfoReq, targetDbData);
        mchInfoService.insert(targetDbData);
    }

    @Override
    public void update(@RequestBody MchInfoReq mchInfoReq) {
        MchInfo mchInfo = mchInfoService.getById(mchInfoReq.getId());
        if (mchInfo == null) {
            throw RRException.build("mchInfo.not.exist");
        }
        if (!mchInfo.getUserName().equals(mchInfoReq.getUserName())) {
            LambdaQueryWrapper<MchInfo> wrapper = new LambdaQueryWrapper();
            wrapper.eq(MchInfo::getUserName, mchInfoReq.getUserName());
            List<MchInfo> targetDbResultList = mchInfoService.selectList(wrapper);
            if (CollUtil.isNotEmpty(targetDbResultList)) {
                throw RRException.build("mchInfo.exist");
            }
        }
        MchInfo targetMchInfo = new MchInfo();
        BeanUtil.copyProperties(mchInfoReq, targetMchInfo);
        mchInfoService.updateById(targetMchInfo);
    }

    @Override
    public void deleteBatchIds(@RequestBody List<Long> ids) {
        LambdaQueryWrapper<MchInfo> wrapper = new LambdaQueryWrapper();
        wrapper.in(MchInfo::getId, ids);
        List<MchInfo> targetDbResultList = mchInfoService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            targetDbResultList.forEach(payType -> {
                payType.setDelFlag(SuperEntity.DEFAULT_DELETE_FLAG);
            });
            mchInfoService.updateBatchById(targetDbResultList);
        }
    }
}
